package com.mazaiting.manager.controller;


import com.mazaiting.common.core.constant.ExMessageConst;
import com.mazaiting.common.core.domain.result.Option;
import com.mazaiting.common.core.ex.exceptions.BusinessException;
import com.mazaiting.common.core.ex.exceptions.EmptyObjectException;
import com.mazaiting.manager.domain.form.DeptForm;
import com.mazaiting.manager.domain.query.DeptQuery;
import com.mazaiting.manager.domain.vo.dept.DeptDetailVO;
import com.mazaiting.manager.domain.vo.dept.DeptVO;
import com.mazaiting.manager.service.ISysDeptService;
import com.mazaiting.web.auth.security.annotation.PrePermission;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * 部门控制器
 *
 * @author mazaiting
 * @since 2022-09-20
 */
@Slf4j
@RestController
@RequestMapping("/manager/v1/depts")
@RequiredArgsConstructor
public class SysDeptController {

    private final ISysDeptService sysDeptService;

    /**
     * 部门列表
     * @param queryParams 查询参数
     * @return 部门列表
     */
    @PrePermission("sys:dept:query")
    @GetMapping
    public List<DeptVO> list(DeptQuery queryParams) {
        return sysDeptService.list(queryParams);
    }

    /**
     * 获取部门下拉选项
     * @return 部门下拉选项
     */
    @GetMapping("options")
    public List<Option<Long>> listDeptOptions() {
        return sysDeptService.listDeptOptions();
    }

    /**
     * 新增部门
     * @param formData 表单数据
     * @return 数据 ID
     */
    @PrePermission("sys:dept:add")
    @PostMapping
    public boolean save(@Valid DeptForm formData) throws BusinessException {
        return sysDeptService.save(formData);
    }

    /**
     * 获取部门详情
     * @param deptId 部门 ID
     * @return  部门 详情
     */
    @PrePermission("sys:dept:update")
    @GetMapping("/{deptId}")
    public DeptDetailVO getDeptDetail(@PathVariable Long deptId) throws EmptyObjectException {
        return sysDeptService.getDeptDetail(deptId);
    }

    /**
     * 修改部门
     * @param formData 表单数据
     * @return 更新部门
     */
    @PrePermission("sys:dept:update")
    @PutMapping
    public boolean update(@Valid DeptForm formData) throws EmptyObjectException {
        return sysDeptService.update(formData);
    }


    /**
     * 删除部门
     * @param ids 部门ID，多个以英文逗号(,)分割
     */
    @PrePermission("sys:dept:delete")
    @DeleteMapping("/{ids}")
    public boolean deleteDepartments(
            @NotBlank(message = ExMessageConst.ERROR_DEPT_IDS_IS_BLANK)
            @PathVariable("ids") String ids
    ) {
        return sysDeptService.deleteByIds(ids);
    }

}
